{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "8ba08215-4f1b-4253-a93c-786673b050bf",
   "metadata": {},
   "source": [
    "## Four ways to multiply $AB = C$ in Julia.\n",
    "\n",
    "This exercise aims to introduce you to arrays (matrices and vectors) in Julia, and show you some ways to manipulate their rows and columns (called array slicing).\n",
    "\n",
    "It will also challenge you to think about the four ways to multiply two matrices!\n",
    "\n",
    "Let's define two matrices $A$ and $B$. $A$ will be $3\\times 2$ and $B$ will be $2\\times 4$.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "ab7e9ee6-7f8c-4653-979b-a6e4647f1d5f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4×2 Matrix{Int64}:\n",
       "  1  1\n",
       "  2  1\n",
       " -1  2\n",
       " -2  2"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = [1 1 ; 2 1; -1 2; -2 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "4d8eed36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2×4 Matrix{Int64}:\n",
       " 1  0  -1  -2\n",
       " 1  1   0   0"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B = [1 0 -1 -2; 1 1 0 0]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "84b1e1ad-1afe-4b07-94cf-9ca79d6302fb",
   "metadata": {},
   "source": [
    "What is the matrix $C = AB$? In Julia, matrix-matrix multiplication is performed with `C = A * B`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "df490981-81e2-408e-b850-fa5f560a4ac2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4×4 Matrix{Int64}:\n",
       " 2  1  -1  -2\n",
       " 3  1  -2  -4\n",
       " 1  2   1   2\n",
       " 0  2   2   4"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C = A*B"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "81f7a5f4",
   "metadata": {},
   "source": [
    "Now, let's explore the four ways to multiply $AB = C$ (textbook pg. 35).\n",
    "\n",
    "**Method 1: Dot product way.** (Row $i$ of $A$) $\\cdot$ (Column $k$ of $B$) = (Number $C_{ik}$)\n",
    "\n",
    "(Note: The notation `A[i,:]` extracts the $i^{th}$ row of $A$ as a *column vector* in Julia. Use `A[i,:]'` to turn it back into a row vector.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "90203164",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4×4 Matrix{Float64}:\n",
       " 2.0  1.0  -1.0  -2.0\n",
       " 3.0  1.0  -2.0  -4.0\n",
       " 1.0  2.0   1.0   2.0\n",
       " 0.0  2.0   2.0   4.0"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "C1 = zeros(4,4)\n",
    "for i in 1:4                                # loop over rows of A\n",
    "    for k in 1:4                            # loop over columns of B\n",
    "        C1[i,k] = A[i,:]'*B[:,k]            # dot product of row i and column k\n",
    "    end\n",
    "end\n",
    "display(C1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a6d3ff5",
   "metadata": {},
   "source": [
    "**Method 2: Column way.** $A \\,\\times$ (Column $k$ of $B$) = (Column $k$ of $C$)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "7c6730dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4×4 Matrix{Float64}:\n",
       " 2.0  1.0  -1.0  -2.0\n",
       " 3.0  1.0  -2.0  -4.0\n",
       " 1.0  2.0   1.0   2.0\n",
       " 0.0  2.0   2.0   4.0"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "C2 = zeros(4,4)\n",
    "for k in 1:4                # loop over columns of B\n",
    "    C2[:,k] = A*B[:,k]      # A times column k of B\n",
    "end\n",
    "display(C2)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "8f4ed3c4",
   "metadata": {},
   "source": [
    "**Method 3: Row way.** (Row $i$ of $A$) $\\times$ $B$ = (Row $i$ of $C$)\n",
    "\n",
    "(Note: The notation `A[i,:]` extracts the $i^{th}$ row of $A$ as a *column vector* in Julia. Use `A[i,:]'` to turn it back into a row vector.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "b5e5ffd9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4×4 Matrix{Float64}:\n",
       " 2.0  1.0  -1.0  -2.0\n",
       " 3.0  1.0  -2.0  -4.0\n",
       " 1.0  2.0   1.0   2.0\n",
       " 0.0  2.0   2.0   4.0"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "C3 = zeros(4,4)\n",
    "for i in 1:4                # loop over rows of A\n",
    "    C3[i,:] = A[i,:]'*B     # row i of A times B\n",
    "end\n",
    "display(C3)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "61b63fc7",
   "metadata": {},
   "source": [
    "**Method 4: Columns times rows.** (Column $j$ of $A$) $\\times$ (Row $j$ of $B$) = (Rank 1 matrix). The sum of the rank 1 matrices is $C$.\n",
    "\n",
    "(Note: The notation `A[i,:]` extracts the $i^{th}$ row of $A$ as a *column vector* in Julia. Use `A[i,:]'` to turn it back into a row vector.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "a1b99050",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4×4 Matrix{Float64}:\n",
       " 2.0  1.0  -1.0  -2.0\n",
       " 3.0  1.0  -2.0  -4.0\n",
       " 1.0  2.0   1.0   2.0\n",
       " 0.0  2.0   2.0   4.0"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "C4 = zeros(4,4)\n",
    "for j in 1:4                    # loop over rows of A and columns of B\n",
    "    C4 = A[j,:]'*B[:,j]         # add row i of A times column j of B\n",
    "end\n",
    "display(C3)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.6.3",
   "language": "julia",
   "name": "julia-1.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
